import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/entity/select_model.dart';
import 'package:flutter_shop/pages/base/base_state_less_widget.dart';
import 'package:flutter_shop/pages/cart/cart_cons.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/entity/cart_goods_data.dart';
import 'package:flutter_shop/pages/cart/order_confirm/order_confirm_presenter.dart';
import 'package:flutter_shop/pages/common/common_input_page/common_input_page.dart';
import 'package:flutter_shop/pages/common/index_page/provider/index_page_provider.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/custom_bottom_sheet.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/single_line_item.dart';
import 'package:provider/provider.dart';

///Title:订单信息 <br />
///Description: <br />
///author:xiemiao <br />
///date: 2020-03-09 <br />
///version 1.0 <br />
class OrderInfo extends BaseStateLessWidget<OrderConfirmPresenter> {
  final List<CartGoodsData> list;

  OrderInfo({this.list});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          _topInfo(context),
          Gaps.v_gap_5,
          _orderList(context),
        ],
      ),
    );
  }

  ///顶部信息
  Widget _topInfo(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          SingleLineItem(
            leftText: "姓名",
            hint: "请输入",
            rightText: mPresenter.mForm.name,
            onPressed: () {
              NavigatorUtils.goCommonInputPage(
                context,
                title: "姓名",
                content: mPresenter.mForm.name,
                maxLength: 10,
                inputType: CommonInputPage.INPUT_TYPE_ARR[0],
                callback: (content) {
                  mPresenter.mForm.name = content;
                  mPresenter.updateStateWidget();
                },
              );
            },
          ),
          SingleLineItem(
            leftText: "手机",
            hint: "请输入",
            rightText: mPresenter.mForm.phone,
            onPressed: () {
              NavigatorUtils.goCommonInputPage(
                context,
                title: "手机",
                content: mPresenter.mForm.phone,
                maxLength: 11,
                inputType: CommonInputPage.INPUT_TYPE_ARR[1],
                callback: (content) {
                  mPresenter.mForm.phone = content;
                  mPresenter.updateStateWidget();
                },
              );
            },
          ),
          SingleLineItem(
            leftText: "地址",
            hint: "请输入",
            rightText: mPresenter.mForm.address,
            onPressed: () {
              NavigatorUtils.goCommonInputPage(
                context,
                title: "地址",
                content: mPresenter.mForm.address,
                maxLength: 100,
                inputType: CommonInputPage.INPUT_TYPE_ARR[0],
                callback: (content) {
                  mPresenter.mForm.address = content;
                  mPresenter.updateStateWidget();
                },
              );
            },
          ),
          SingleLineItem(
            leftText: "预定时间",
            hint: "请选择",
            rightText: mPresenter.mForm.reserveTime,
            onPressed: () {
              Utils.showYMDHMPicker(context, ymdhmStr: mPresenter.mForm.reserveTime, callback: (val) {
                mPresenter.mForm.reserveTime = val;
                mPresenter.updateStateWidget();
              });
            },
          ),
          SingleLineItem(
            leftText: "支付方式",
            hint: "请选择",
            rightText: mPresenter.mForm.payWayStr,
            onPressed: () {
              Utils.showCustomBottomSheet(
                  context,
                  ListSelectSheet(
                    [
                      SelectModel(CartCons.PAY_WAY_ARR[0], "支付宝"),
                      SelectModel(CartCons.PAY_WAY_ARR[1], "微信"),
                    ],
                    title: "支付方式",
                    callback: (val) {
                      mPresenter.mForm.payWay = val.id;
                      mPresenter.updateStateWidget();
                    },
                  ));
            },
          ),
        ],
      ),
    );
  }

  ///订单列表
  Widget _orderList(BuildContext context) {
    //获取定位城市
    String currentCity = Provider.of<IndexPageProvider>(context).currentCity;
    //计算小计价格
    double subtotal = 0;
    list.forEach((item) {
      double price = NumUtil.multiply(item.price, item.count);
      subtotal = NumUtil.add(subtotal, price);
    });

    return Container(
      child: Column(
        children: <Widget>[
          SingleLineItem(
            leftText: "百姓量贩($currentCity)",
            editable: false,
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: list.length,
            itemBuilder: _buildItem,
          ),
          Container(
            padding: Paddings.common,
            color: Colors.white,
            alignment: Alignment.centerRight,
            child: Text("小计:  ¥$subtotal"),
          ),
          Gaps.v_gap_15,
        ],
      ),
    );
  }

  ///item
  Widget _buildItem(context, index) {
    CartGoodsData item = list[index];
    return Container(
      padding: Paddings.common,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Borders.bottom,
      ),
      child: Row(
        children: <Widget>[
          LoadImage(
            item.image,
            width: 40,
            height: 40,
            fit: BoxFit.cover,
          ),
          Gaps.h_gap_10,
          Expanded(child: Text(item.goodsName, style: TextStyles.common_text)),
          Text("x${item.count}", style: TextStyles.common_text),
          Container(
            width: ScreenUtil().setWidth(150),
            alignment: Alignment.centerRight,
            child: Text("¥${item.price}", style: TextStyles.common_text),
          ),
        ],
      ),
    );
  }
}
